# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import Plugin

class MainClass( Plugin.Plugin ):
    '''Main plugin class'''
    
    description = _('Open conversation window when user starts typing.')
    authors = { 'Jan de Mooij' : 'jandemooij@gmail.com' }
    displayName = _('Psycho Mode')
    name = 'PsychoMode'
    def __init__( self, controller, msn ):
        '''Contructor'''

        Plugin.Plugin.__init__( self, controller, msn )

        self.description = _('Open conversation window when user starts typing.')
        self.authors = { 'Jan de Mooij' : 'jandemooij@gmail.com' }
        self.displayName = _('Psycho Mode')
        self.name = 'PsychoMode'
        
        self.controller = controller
        self.config = controller.config
        self.config.readPluginConfig(self.name)

        self.handlerId = None
        self.enabled = False
    
    def receiveTyping(self, msn, switchboard, signal, args):
        controller = self.controller
        mail = args[0]
        
        result = controller.conversationManager.getOpenConversation(mail, \
            switchboard)
        if result == None:
            window, conversation = controller.newConversation(self.msn, \
                mail, switchboard)

            template = self.config.getPluginValue(self.name, 'message', \
                _('%s starts typing...'))
            try:
                message = template % mail
            except TypeError:
                message = template

            conversation.appendOutputText(None, message, 'information')
            conversation.receiveTyping(switchboard, mail)
    
    def start( self ):
        '''start the plugin'''
        self.handlerId = self.connect('switchboard::typing',
            self.receiveTyping)
        self.enabled = True
        
    def stop( self ):    
        '''stop the plugin'''
        self.controller.msn.disconnect(self.handlerId)
        self.enabled = False
        
    def check(self):
        return (True, 'Ok')

    def configure(self):
        opts = [Plugin.Option('message', str, 'Notification message:', '', \
            self.config.getPluginValue(self.name, 'message', \
            _('%s starts typing...')))]
        result = Plugin.ConfigWindow(_('Psycho mode'), opts).run()

        if result and 'message' in result and result['message'].value:
            self.config.setPluginValue(self.name, 'message', \
                result['message'].value)
